package com.igeek.d0605;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: zhiyu
 * @Date:18:11
 * @DESC:
 */
public class StreamApiDemo {
    public static void main(String[] args) {
        //1.练习Stream中的常用方法of()、iterate()、generate()、findFirst()、findAny()、anyMatch()、distinct()等
        Stream.of(1,1,2,32,342,3,34,32);
        Stream.iterate(0,i->i+1).limit(10);
        Stream.generate(()->"hello").limit(10);
        Stream.of(1,23,32,5,6,5,6,56).findFirst();
        Stream.of(1,23,32,5,6,5,6,56).filter(i->i>50).findAny();
        Stream.of(1,23,32,5,6,5,6,56).anyMatch(i->i>50);
        Stream.of(1,23,32,5,6,5,6,56).allMatch(i->i>50);
        Stream.of(1,23,32,5,6,5,6,56).distinct();
        //2.筛选出Integer集合中大于7的元素，并打印，例如6,7,3,8,1,2,9
        Integer[] arr = {6,7,3,8,1,2,9};
        Arrays.asList(arr).stream().filter(i->i>7).forEach(System.out::println);
        //3.筛选员工中工资高于8000的人，并形成新的集合
        Employee e1 = new Employee("张三", 18888.0,18);
        Employee e2 = new Employee("李四", 10888.0,23);
        Employee e3 = new Employee("王五", 7888.0,25);
        Employee e4 = new Employee("赵六", 7888.0,24);
        Stream.of(e1,e2,e3).filter(e->e.getSalary()>8000).collect(Collectors.toList()).forEach(System.out::println);
        //4.获取String集合中最长的元素
        String[] str = {"wqeqw","awqw","erwe","er","fsfssfs"};
        Optional<String> max = Arrays.asList(str).stream().max(Comparator.comparingInt(String::length));
        System.out.println("max = " + max.orElse(null));
        //5.获取Integer集合中最大值
        Optional<Integer> max1 = Arrays.asList(arr).stream().max(Comparator.comparingInt(Integer::intValue));
        System.out.println("max1.orElse(null) = " + max1.orElse(null));
        //6.获取员工工资最高的人
        Optional<Employee> max2 = Stream.of(e1, e2, e3).max(Comparator.comparingDouble(Employee::getSalary));
        System.out.println("max2.orElse(null) = " + max2.orElse(null));
        //7.计算Integer集合中大于6的元素的个数
        Long collect = Arrays.asList(arr).stream().filter(i -> i > 6).collect(Collectors.counting());
        System.out.println("collect = " + collect);
        //8.将员工按照工资由低到高排序，显示名字
        Stream.of(e1,e2,e3).sorted(Comparator.comparingDouble(Employee::getSalary).reversed()).forEach(System.out::println);
        //9.将员工按照工资由高到低排序，显示名字
        Stream.of(e1,e2,e3).sorted(Comparator.comparingDouble(Employee::getSalary)).forEach(System.out::println);
        //10.将员工按照工资由低到高（工资一样则按年龄由小到大）排序，显示名字
        Stream.of(e1,e2,e3,e4).sorted(Comparator.comparingDouble(Employee::getSalary).thenComparingInt(Employee::getAge)).forEach(System.out::println);
    }
}
